<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="../res/layui/css/layui.css">
</head>
<body>
<div class="layui-fluid">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>管理员新增</legend>
        <div class="layui-field-box">
            <div class="layui-col-md3">
                <form class="layui-form">
                    <div class="demo-reg-container">
                        <div class="layui-form-item">
                            <div class="layui-input-wrap">
                                <div class="layui-input-prefix">
                                    <i class="layui-icon layui-icon-username"></i>
                                </div>
                                <input type="text" name="username" value="" lay-verify="required" lay-vertype="tips" lay-reqtext="请输入账号" placeholder="账号" autocomplete="off" class="layui-input" lay-affix="clear">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-wrap">
                                <div class="layui-input-prefix">
                                    <i class="layui-icon layui-icon-password"></i>
                                </div>
                                <input type="password" name="password" value="" lay-verify="required" lay-vertype="tips" lay-reqtext="请输入密码" placeholder="密码" autocomplete="off" class="layui-input" id="reg-password" lay-affix="eye">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-wrap">
                                <div class="layui-input-prefix">
                                    <i class="layui-icon layui-icon-password"></i>
                                </div>
                                <input type="password" name="confirmPassword" value="" lay-verify="required|confirmPassword" lay-vertype="tips" lay-reqtext="请输入确认密码" placeholder="确认密码" autocomplete="off" class="layui-input" lay-affix="eye">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="demo-reg">创建账号</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </fieldset>
</div>
<script src="../res/layui/layui.js"></script>
<script>
    layui.use(['form', 'layer', 'jquery'], function () {
        var form = layui.form;
        var layer = layui.layer;
        var $ = layui.jquery;

        // 自定义验证规则
        form.verify({
            // 确认密码
            confirmPassword: function(value, item){
                var passwordValue = $('#reg-password').val();
                if(value !== passwordValue){
                    return '两次密码输入不一致';
                }
            }
        });

        // 提交事件
        form.on('submit(demo-reg)', function(data){
            var field = data.field; // 获取表单字段值

            // 显示填写结果，仅作演示用
            layer.alert(JSON.stringify(field), {
                title: '当前填写的字段值'
            });

            // 发送 AJAX 请求
            $.ajax({
                url: '/admin/add', // 替换为实际的 API 地址
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify(field),
                success: function (response) {
                    if (response.code === 0) {
                        layer.msg('账号创建成功');
                    } else {
                        layer.msg('账号创建失败：' + response.message);
                    }
                },
                error: function (xhr, status, error) {
                    layer.msg('请求失败：' + error);
                }
            });

            return false; // 阻止默认 form 跳转
        });
    });
</script>
</body>
</html>
